package com.runmi.webchat.aftersale.web.iframe.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.runmi.webchat.aftersale.common.result.Result;
import com.runmi.webchat.aftersale.common.result.ResultFactory;
import com.runmi.webchat.aftersale.common.utils.MD5;
import com.runmi.webchat.aftersale.common.utils.StringUtil;
import com.runmi.webchat.aftersale.dbcore.entity.AdminMenu;
import com.runmi.webchat.aftersale.dbcore.entity.User;
import com.runmi.webchat.aftersale.dbcore.mapper.AdminMenuMapper;
import com.runmi.webchat.aftersale.dbcore.mapper.RoleMapper;
import com.runmi.webchat.aftersale.dbcore.mapper.RoleMenuMapper;
import com.runmi.webchat.aftersale.dbcore.mapper.UserMapper;
import com.runmi.webchat.aftersale.util.Constants;
import com.runmi.webchat.aftersale.web.base.BaseService;
import com.runmi.webchat.aftersale.web.iframe.model.MainLeft;
import com.runmi.webchat.aftersale.web.iframe.model.MainLeftMenu;


@Service
public class MainService  extends BaseService{


	@Autowired
	private AdminMenuMapper adminMenuMapper;
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private RoleMapper roleMapper;
	
	@Autowired
	private RoleMenuMapper roleMenuMapper;
	
	public void left(MainLeft model){		
		//Result result=ResultFactory.getResultFail();
		try{
			
			User wmsAdmin=userMapper.selectByPrimaryKey(model.getUserId());	
			Integer roleId=wmsAdmin.getRoleId();
			roleId=roleId==null?0:roleId;
			List<AdminMenu> allmenus= adminMenuMapper.getUserMenuByRoleId(roleId);
			List<MainLeftMenu> menus=new ArrayList<MainLeftMenu>();
			List<AdminMenu> menu1=getAdminMenu(allmenus,0);
			for(int i=0;i<menu1.size();i++){
				AdminMenu menuAdmin=menu1.get(i);
				
					MainLeftMenu menu=new MainLeftMenu();
					menu.setAdminMenu(menuAdmin);
					menu.setItems(getAdminMenu(allmenus,menuAdmin.getId()));
					if(null!=menu.getItems() && menu.getItems().size()>0){
						menu.getAdminMenu().setIsDisMenu((byte)1);
					}else{
						menu.getAdminMenu().setIsDisMenu((byte)0);
					}
					menus.add(menu);
				
			}
			model.setMenus(menus);
			//result=ResultFactory.getResultListSuccess(menus);
			//model.setMenus(menus);
			
		}catch(Exception ex){
			this.logErr.error("left menue error" +model.getUserId(),ex);
		}
		//return result;
	}
	private List<AdminMenu> getAdminMenu(List<AdminMenu> menus,Integer parentId){
		List<AdminMenu> result=new ArrayList<AdminMenu>();
		for(int i=0;i<menus.size();i++){
			AdminMenu menu=menus.get(i);//administrator   						
				if(menu.getParentId()==parentId){
					result.add(menu);
			}
			
		}
		result.sort((h1,h2)->h1.getSort().compareTo(h2.getSort()));
		return result;
	}
	
	public Map<String,String>  modifyPwd(User erpUser,String oldPwd,String newPwd1,String newPwd2){
		Map<String,String> result=new HashMap<String,String>();
		try{
			this.log.info("erpUser:" + erpUser.getUserName() + "修改密码");
			if(StringUtil.isBlank(oldPwd) || StringUtil.isBlank(newPwd1) || StringUtil.isBlank(newPwd2)){
				result.put(Constants.RETURN_CODE_KEY, "0200");
				result.put(Constants.RETURN_MESSAGE_KEY, "请输入密码");	
				return result;
			}
			
			String psdmd5=MD5.getMD5(oldPwd);
			if(!erpUser.getPassword().equals(psdmd5)){		
				result.put(Constants.RETURN_CODE_KEY, "0200");
				result.put(Constants.RETURN_MESSAGE_KEY, "旧密码错误");	
				return result;
			}
			if(!newPwd1.equals(newPwd2)){
				result.put(Constants.RETURN_CODE_KEY, "0200");
				result.put(Constants.RETURN_MESSAGE_KEY, "两次密码不一致");	
				return result;
			}
			String newpsdmd5=MD5.getMD5(newPwd1);
			User record=new User();
			record.setId(erpUser.getId());
			record.setPassword(newpsdmd5);
			record.setUpdateTime(new Date());
			userMapper.updateByPrimaryKeySelective(record);
		
			result.put(Constants.RETURN_CODE_KEY,Constants.RETURN_CODE_SUCCESS);
			result.put(Constants.RETURN_MESSAGE_KEY, Constants.RETURN_CODE_SUCCESS_MESSAGE);	
		
		}catch(Exception ex){
			this.logErr.error("修改密码 " +erpUser.getId(),ex);
			ex.printStackTrace();
		}
		return result;
	}

}
